

import {focusCancelUser, focusUser, getUserInfo} from "../../../api/um";

const globalData = getApp().globalData;
const {colorOption, config, userInfo} = globalData;


Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo,
    colorOption,
    // 名片数据
    cardData: {},
    tagListMap: {},
    userId: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const {id} = options;
    let userId = id || userInfo.id;
    this.setData({userId})
    this.getData(userId);
  },

  // 获取用户数据
  getData(id) {
    wx.showLoading({title: '加载中',})
    const noTag = ['XQAH_str', 'XB_str']
    getUserInfo(id, (res) => {
      console.log(res);
      let {data} = res
      let {attrDataMap} = res.data
      if (attrDataMap.XC) {
        attrDataMap.XC = attrDataMap.XC.split(',').map(s => config.localImgSrc + s);
      } else {
        attrDataMap.XC = [];
      }
      attrDataMap.TX = attrDataMap.TX ? config.localImgSrc + attrDataMap.TX : (attrDataMap.XB == 1 ? '/images/mine/defaultAvatar_male.png' : '/images/mine/defaultAvatar_female.png');
      attrDataMap.XC_limit = attrDataMap.XC.slice(0, 2)
      const tagListMap = {};
      const {attrConfMap} = globalData
      Object.keys(attrConfMap).forEach(key=>{
        attrConfMap[key].forEach(conf=>{
          const code = conf.attrCode + "_str";
          if (!tagListMap[key]) tagListMap[key] = [];
          noTag.indexOf(code) < 0 && attrDataMap[code] && tagListMap[key].push(attrDataMap[code])
        })
      })
      data.hobbyList = attrDataMap.XQAH_str ? attrDataMap.XQAH_str.split("丨") : [];
      this.setData({
        cardData: data,
        tagListMap,
      });
      wx.hideLoading();
    })
  },

  // 关注按钮
  handleFocus() {
    wx.showLoading({title: '操作中',})
    let fun = this.data.cardData.isFocus ? focusCancelUser : focusUser;
    fun(this.data.userId, () => {
      this.getData(this.data.userId);
    });
  },

  // 预览图片
  previewImage(e) {
    const {index, list} = e.currentTarget.dataset;

    wx.previewImage({
      current: list[index],
      urls: list,
    });
  },

  goBack() {
    wx.navigateBack()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
  },
});
